package com.tsll.controller;


import com.tsll.entity.Book;
import com.tsll.service.BookComService;
import com.tsll.service.BookService;
import com.tsll.service.BookTypeService;
import com.tsll.vo.BackVo;
import com.tsll.vo.BookVo;
import com.tsll.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;





@RestController
@RequestMapping("/book")
public class BookController {
    @Autowired
    private BookService bookService;
    @Autowired
    private BookComService bookComService;
    @Autowired
    private BookTypeService bookTypeService;

    //获取全部实体图书信息,传入分页数据和搜索数据
    @PostMapping("/list/{current}/{pageSize}")
    public Result list(@PathVariable Integer current, @PathVariable Integer pageSize, @RequestBody BookVo bookVo) {
        System.out.println("=====bookVo=====");
        System.out.println(bookVo);
        return bookService.findByPage(current, pageSize, bookVo);
    }
    //获取全部图书类型信息
    @RequestMapping("/booktype")
    public Result booktype() {
        return bookTypeService.finAll();
    }

    //添加图书功能
    @RequestMapping("/add")
    public Result add(@RequestBody Book book) {
        return bookService.insert(book);
    }
    //更改图书信息
    @RequestMapping("/update")
    public Result update(@RequestBody Book book) {
        return bookService.update(book);
    }

    //删除图书信息
    @RequestMapping("/del/{bookId}")
    public Result del(@PathVariable Integer bookId) {
        return bookService.delBook(bookId);
    }

    //按图书借阅次数排行的受欢迎图书信息
    @RequestMapping("/getAllPopulars")
    public Result getAllPopulars() {
        return bookService.getpopulars();
    }

    //最近半年内新发布的图书信息
    @RequestMapping("/newDate")
    public Result newDate() {
        return bookService.AllNewDate();
    }
    //精彩评论内应包含的信息
    @RequestMapping("/rowCom/{bookId}")
    public Result rowCom(@PathVariable Integer bookId){
          return bookComService.getComById(bookId);
    }
    //获取借阅审核图书信息
    @PostMapping("/borrow/{current}/{pageSize}")
    public Result getBorrows(@PathVariable Integer current, @PathVariable Integer pageSize, @RequestBody BookVo bookVo){
        return bookService.getBorrow(current,pageSize,bookVo);
    }

    //查询所用损坏的图书信息
    @RequestMapping("/selectAll/{current}/{pageSize}")
    public  Result selectAll(@PathVariable Integer current, @PathVariable Integer pageSize){
        return bookService.selectAll(current,pageSize);
    }

    //根据图书类型查询图书
    @RequestMapping("/selectByBookTypeId/{id}")
    public  Result selectByBookTypeId(@PathVariable int id){
        return bookService.selectByBookTypeId(id);
    }


    //查询图书总数
    @PostMapping("/getbookcount")
    public Result getbookcounts(@RequestBody BookVo bookVo) {
        return bookService.getbookcount(bookVo);
    }

    //根据id查询书籍
    @RequestMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id){
        return new Result(200,"查询书籍成功",bookService.selectById(id));
    }

    @RequestMapping("/backBook")
    public Result backBookById(@RequestBody BackVo backVo){
        System.out.println("归还图书");
        System.out.println(backVo);
        return bookService.backBook(backVo);
    }

    //根据图书id查询图书内容
    @RequestMapping("/readBook/{id}")
    public Result readBook(@PathVariable Integer id){
        return bookService.readBook(id);
    }

}
